package com.sojson.controller.po;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.WorkExperienceParamVerify;
import com.sojson.entity.po.WorkExperience;
import com.sojson.result.Result;
import com.sojson.service.WorkExperienceService;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 工作经历表控制器
 * 
 * @author liu
 * @date 2021-04-26
 */
@Api(tags = "工作经历表管理")
@RestController
@RequestMapping("/workExperience")
public class WorkExperienceController {

    @Autowired
    private WorkExperienceService workExperienceService;

    /**
     * 单条插入
     * 
     * @param workExperience
     * @return
     * @throws IOException 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.login()")
    public Object insert(@RequestBody WorkExperience workExperience) throws IOException {
        // 校验参数
        Result<?> paramResult = WorkExperienceParamVerify.insert(workExperience);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        workExperience.insertInit();
        return Result.success(workExperienceService.insert(workExperience));
    }

    /**
     * 批量插入
     * 
     * @param workExperience
     * @return
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.insertList(workExperience));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param workExperience
     * @return
     */
    // @DeleteMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.deleteByDto(workExperience));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    // @GetMapping("/countAll")
    public Object countAll() {
        return Result.success(workExperienceService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    // @GetMapping("/findAll")
    public Object findAll() {
        return Result.success(workExperienceService.findAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param workExperience
     * @return
     */
    // @GetMapping("/countByDto")
    public Object countByDto(WorkExperience workExperience) {
        return Result.success(workExperienceService.countByDto(workExperience));
    }

    /**
     * 根据对象查询
     * 
     * @param workExperience
     * @return
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/workExperience/findByDto')")
    public Object findByDto(WorkExperience workExperience) {
        return Result.success(workExperienceService.findByDto(workExperience));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.login()")
    public Object deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(workExperienceService.deleteById(id));
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    // @DeleteMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(workExperienceService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateById")
    public Object updateById(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateById(workExperience));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateStaticByIdList(workExperience));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateChangeByIdList(workExperience));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param workExperience
     * @return
     * @throws IOException 
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.login()")
    public Object updatePartialById(@RequestBody WorkExperience workExperience) throws IOException {
        // 校验参数
        Result<?> paramResult = WorkExperienceParamVerify.updatePartialById(workExperience);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        workExperience.updateInit();
        return Result.success(workExperienceService.updatePartialById(workExperience));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateStaticPartialByIdList(workExperience));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateChangePartialByIdList(workExperience));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Object findById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(workExperienceService.findById(id));
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(workExperienceService.findByIdList(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param workExperience
     * @return
     */
    // @GetMapping("/findIdByDto")
    public Object findIdByDto(WorkExperience workExperience) {
        return Result.success(workExperienceService.findIdByDto(workExperience));
    }

    /**
     * 通过userId删除(物理删除)
     * 
     * @param userId
     * @return
     */
    // @DeleteMapping("/deleteByUserId")
    public Object deleteByUserId(Integer userId) {
        if (userId == null) {
            return Result.success();
        }

        return Result.success(workExperienceService.deleteByUserId(userId));
    }

    /**
     * 通过userId列表删除(物理删除)
     * 
     * @param userIds
     * @return
     */
    // @DeleteMapping("/deleteByUserIdList")
    public Object deleteByUserIdList(@RequestParam("userIds") List<Integer> userIds) {
        if (userIds.size() == 0) {
            return Result.success();
        }

        return Result.success(workExperienceService.deleteByUserIdList(userIds));
    }

    /**
     * 通过userId更新(全部更新)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateByUserId")
    public Object updateByUserId(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateByUserId(workExperience));
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateStaticByUserIdList")
    public Object updateStaticByUserIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateStaticByUserIdList(workExperience));
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容不相同)
     * 
     * @param workExperiences
     * @return
     */
    // @PutMapping("/updateChangeByUserIdList")
    public Object updateChangeByUserIdList(@RequestBody WorkExperience workExperiences) {
        return Result.success(workExperienceService.updateChangeByUserIdList(workExperiences));
    }

    /**
     * 通过userId更新(局部更新)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updatePartialByUserId")
    public Object updatePartialByUserId(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updatePartialByUserId(workExperience));
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容相同)
     * 
     * @param workExperience
     * @return
     */
    // @PutMapping("/updateStaticPartialByUserIdList")
    public Object updateStaticPartialByUserIdList(@RequestBody WorkExperience workExperience) {
        return Result.success(workExperienceService.updateStaticPartialByUserIdList(workExperience));
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容不相同)
     * 
     * @param workExperiences
     * @return
     */
    // @PutMapping("/updateChangePartialByUserIdList")
    public Object updateChangePartialByUserIdList(@RequestBody WorkExperience workExperiences) {
        return Result.success(workExperienceService.updateChangePartialByUserIdList(workExperiences));
    }

    /**
     * 通过userId查询
     * 
     * @param userId
     * @return
     */
    // @GetMapping("/findByUserId")
    public Object findByUserId(Integer userId) {
        if (userId == null) {
            return Result.success();
        }

        return Result.success(workExperienceService.findByUserId(userId));
    }

    /**
     * 通过userId列表查询
     * 
     * @param userIds
     * @return
     */
    // @GetMapping("/findByUserIdList")
    public Object findByUserIdList(@RequestParam("userIds[]") List<Integer> userIds) {
        if (userIds.size() == 0) {
            return Result.success();
        }

        return Result.success(workExperienceService.findByUserIdList(userIds));
    }

    /**
     * 通过对象查询userId列表
     * 
     * @param workExperience
     * @return
     */
    // @GetMapping("/findUserIdByDto")
    public Object findUserIdByDto(WorkExperience workExperience) {
        return Result.success(workExperienceService.findUserIdByDto(workExperience));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    // @DeleteMapping("/clear")
    public Object clear() {
        workExperienceService.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param workExperience
     * @return
     * @throws IOException 
     */
    @GetMapping("/findXcxByDto")
    @PreAuthorize("@ss.login()")
    public Object findXcxByDto(WorkExperience workExperience) throws IOException {
        workExperience.setUserId(TokenUtil.getUserId());
        return Result.success(workExperienceService.findByDto(workExperience));
    }

}